﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Course.Models;
using Course.DAO;

namespace Course.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Content()
        {
            ArticleDAO a = new ArticleDAO();
            var query = a.GetArticles("公告");
            var link = a.GetOne("友情链接");

            ViewData["link"] = link;
            ViewData["public"] = query;
            return View();
        }

        public ActionResult Index()
        {
            return View();
        }
                                                                            
        public ActionResult SubjectInfo(string itName)
        {
            ArticleDAO articleDao = new ArticleDAO();
            ViewData["one"] = articleDao.GetOne(itName);

            return View();
        }

        public ActionResult Teach(string itName)
        {
            ArticleDAO articleDao = new ArticleDAO();
            ViewData["one"] = articleDao.GetOne(itName);
            return View();
        }

        public ActionResult Teacher(string itName)
        {
            ArticleDAO articleDao = new ArticleDAO();
            ViewData["one"] = articleDao.GetOne(itName);
            return View();
        }

        public ActionResult Learn(string itName)
        {
            ArticleDAO articleDao = new ArticleDAO();
            ViewData["one"] = articleDao.GetOne(itName);
            return View();
        }

        public ActionResult Resources(string itName)
        {
            ArticleDAO articleDao = new ArticleDAO();
            ViewData["one"] = articleDao.GetOne(itName);
            return View();
        }

        public ActionResult Education(string itName)
        {
            ArticleDAO articleDao = new ArticleDAO();
            ViewData["one"] = articleDao.GetOne(itName);
            return View();
        }

        public ActionResult Exploration(string itName)
        {
            ArticleDAO articleDao = new ArticleDAO();
            ViewData["one"] = articleDao.GetOne(itName);
            return View();
        }

        public ActionResult Opinion(string itName)
        {
            ArticleDAO articleDao = new ArticleDAO();
            ViewData["one"] = articleDao.GetOne(itName);
            return View();
        }

        public ActionResult PublicInfo(int id)
        {
            ArticleDAO articleDao = new ArticleDAO();
            var query = articleDao.GetOne(id);
            ViewData["one"] = query;
            return View();
        }

        public ActionResult DownLoad()
        {
            DownloadDAO downloadDao = new DownloadDAO();
            var query = downloadDao.GetAll();
            ViewData["all"] = query;
            return View();
        }

        public ActionResult DownLoad(string itName)
        {
            DownloadDAO downloadDao=new DownloadDAO();
            var query = downloadDao.GetOneByTitle(itName);
            return View();
        }


        public ActionResult DownLoadList(string itName)
        {
            DownloadDAO downloadDao = new DownloadDAO();
            var query = downloadDao.GetSomeByCategory(itName);
            ViewData["all"] = query;
            return View();
        }

        //public FileStreamResult StreamFileFromDisk()
        //{
        //    string path = AppDomain.CurrentDomain.BaseDirectory + "uploads/";
        //    string fileName = "test.txt";
        //    return File(new FileStream(path + fileName, FileMode.Open), "text/plain", fileName);
        //}

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public FileStreamResult StreamFileFromDisk(string url)
        {
            //string path = url;
            string fileName = DateTime.Now.ToString("yyyyMMddHHmmssff");

            int index = -1;
            for (int i = url.Length - 1; i >= 0; i--)
            {
                if(url[i]=='.')
                {
                    index = i;
                    break;
                }
            }
            string str = url.Substring(index);
            fileName += str;

           
            return File(new FileStream(url, FileMode.Open), "text/plain", fileName);
        }

        public ActionResult DetailCategory(string itName)
        {
            ArticleDAO articleDao=new ArticleDAO();
            var query = articleDao.GetArticles(itName);

            ViewData["all"] = query;
            return View();
        }

        public ActionResult DetailArticle(string itName)
        {
            ArticleDAO articleDao=new ArticleDAO();
            var query = articleDao.GetOneByTitle(itName);

            ViewData["one"] = query;
            return View();
        }

    }
}
